﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1.segments.field
{
    class CE
    {
        private StringBuilder idintifier;
        private StringBuilder text;
        private StringBuilder name_of_coding_system;
        private StringBuilder alternate_identifier;
        private StringBuilder alternate_text;
        private StringBuilder name_of_alternate_coding_system;

        public StringBuilder Identifier { get { return idintifier; } set { idintifier = value; } }
        public StringBuilder Text { get { return text; } set { text = value; } }
        public StringBuilder Name_of_coding_system { get { return name_of_coding_system; } set { name_of_coding_system = value; } }
        public StringBuilder Alternate_identifier { get { return alternate_identifier; } set { alternate_identifier = value; } }
        public StringBuilder Alternate_next { get { return alternate_text; } set { alternate_text = value; } }
        public StringBuilder Name_of_alternate_coding_system { get { return name_of_alternate_coding_system; } set { name_of_alternate_coding_system = value; } }

        public CE()
        {
            idintifier = new StringBuilder();
            text = new StringBuilder();
            name_of_alternate_coding_system = new StringBuilder();
            alternate_identifier = new StringBuilder();
            alternate_text = new StringBuilder();
            name_of_coding_system = new StringBuilder();
        }

        enum segments { DG1, EVN, MSH, OBX, ORC, PID, PV1, RXO, NTE, OBR }

        string check(string source, int temp)
        {

            if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.MSH.ToString())
                return segments.MSH.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.DG1.ToString())
                return segments.DG1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.EVN.ToString())
                return segments.EVN.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.NTE.ToString())
                return segments.NTE.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBR.ToString())
                return segments.OBR.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBX.ToString())
                return segments.OBX.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.ORC.ToString())
                return segments.ORC.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PID.ToString())
                return segments.PID.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PV1.ToString())
                return segments.PV1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.RXO.ToString())
                return segments.RXO.ToString();

            return null;
        }

        public int fill(string source, int temp)
        {
            int i, x = 0;
            for (i = temp; i < source.Length; i++)
            {
                if (check(source, i) != null)
                {
                    idintifier.Capacity = idintifier.Length;
                    text.Capacity = text.Length;
                    name_of_alternate_coding_system.Capacity = name_of_alternate_coding_system.Length;
                    alternate_identifier.Capacity = alternate_identifier.Length;
                    alternate_text.Capacity = alternate_text.Length;
                    name_of_coding_system.Capacity = name_of_coding_system.Length;
                    return i-1;
                }
                else if (source[i] == '^')
                    x++;
                else if (source[i] == '|')
                {
                    idintifier.Capacity = idintifier.Length;
                    text.Capacity = text.Length;
                    name_of_alternate_coding_system.Capacity = name_of_alternate_coding_system.Length;
                    alternate_identifier.Capacity = alternate_identifier.Length;
                    alternate_text.Capacity = alternate_text.Length;
                    name_of_coding_system.Capacity = name_of_coding_system.Length;
                    return i;
                }

                else if (x == 0)
                {
                    if (idintifier == null)
                        idintifier = new StringBuilder();
                    idintifier.Insert(idintifier.Length, source[i]);
                }
                else if (x == 1)
                {
                    if (text == null)
                        text = new StringBuilder();
                    text.Insert(text.Length, source[i]);
                }
                else if (x == 2)
                {
                    if (name_of_coding_system == null)
                        name_of_coding_system = new StringBuilder();
                    name_of_coding_system.Insert(name_of_coding_system.Length, source[i]);
                }
                else if (x == 3)
                {
                    if (alternate_identifier == null)
                        alternate_identifier = new StringBuilder();
                    alternate_identifier.Insert(alternate_identifier.Length, source[i]);
                }
                else if (x == 4)
                {
                    if (alternate_text == null)
                        alternate_text = new StringBuilder();
                    alternate_text.Insert(alternate_text.Length, source[i]);
                }
                else if (x == 5)
                {
                    if (name_of_alternate_coding_system == null)
                        name_of_alternate_coding_system = new StringBuilder();
                    name_of_alternate_coding_system.Insert(name_of_alternate_coding_system.Length, source[i]);
                }

            }
            return i;
        }

        public string tostring()
        {
            string hd_string = "";

            if (idintifier != null)
                hd_string += idintifier;
            if (text != null)
                hd_string += "^" + text;
            if (name_of_coding_system != null)
                hd_string += "^" + name_of_coding_system;
            if (alternate_identifier != null)
                hd_string += "^" + alternate_identifier;
            if (alternate_text != null)
                hd_string += "^" + alternate_text;
            if (name_of_alternate_coding_system != null)
                hd_string += "^" + name_of_alternate_coding_system;
            return hd_string;
        }

    }
}
